/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.iasd.youthshelter.activity.nodes;

import java.awt.event.ActionEvent;
import java.util.ResourceBundle;
import java.util.Set;
import javax.swing.AbstractAction;
import javax.swing.Action;
import static javax.swing.Action.NAME;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import pt.iasd.youthshelter.activity.ActivityEditorTopComponent;
import pt.iasd.youthshelter.datacore.controller.exceptions.IllegalOrphanException;
import pt.iasd.youthshelter.datacore.controller.exceptions.NonexistentEntityException;
import pt.iasd.youthshelter.datamodel.Activity;
import pt.iasd.youthshelter.datamodel.service.ActivityController;
import pt.iasd.youthshelter.datamodel.service.ActivityParticipationController;
import pt.iasd.youthshelter.logging.ILoggingService;

/**
 *
 * @author Rui
 */
public class ActivityNode extends AbstractNode {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/activity/Bundle");
    
    private final Activity activity;
    
    public ActivityNode(Activity activity) {
        super(Children.LEAF);
        this.activity = activity;
        setDisplayName(this.activity.getName());
    }

    @Override
    public Action getPreferredAction() {
        return new OpenActivityAction();
    }

    @Override
    public Action[] getActions(boolean context) {
        return new Action[]{new OpenActivityAction(), new DeleteActivityAction()};
    }
    
    private class OpenActivityAction extends AbstractAction {
        public OpenActivityAction() {
            putValue(NAME, bundle.getString("Action.Open"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            TopComponent editor = new ActivityEditorTopComponent(activity);
            editor.open();
            editor.requestActive();
        }
    }
    
    private class DeleteActivityAction extends AbstractAction {
        public DeleteActivityAction() {
            putValue(NAME, bundle.getString("Action.Delete"));
        }
        
        @Override
        public void actionPerformed(ActionEvent e) {
            ActivityParticipationController participationController = Lookup.getDefault().lookup(ActivityParticipationController.class);
            ActivityController activityController = Lookup.getDefault().lookup(ActivityController.class);
            if(participationController != null && activityController != null) {
                NotifyDescriptor.Confirmation confirm = new NotifyDescriptor.Confirmation(
                        bundle.getString("Delete.Activity.Message"),
                        bundle.getString("Delete.Activity.Title")
                );
                if(DialogDisplayer.getDefault().notify(confirm) == NotifyDescriptor.OK_OPTION) {
                    try {
                        participationController.destroyAll(activity);
                        activityController.destroy(activity.getId());
                        TopComponent editor = findActivityEditorComponent(activity);
                        if(editor != null) {/*
                            while(editor.getUndoRedo().canUndo()) {
                                editor.getUndoRedo().undo();
                            }*/
                            editor.close();
                        }
                    } catch (NonexistentEntityException ex) {
                        Lookup.getDefault().lookup(ILoggingService.class).warn(this.getClass(), ex);
                    } catch (IllegalOrphanException ex) {
                        NotifyDescriptor.Message alert = new NotifyDescriptor.Message(
                        bundle.getString("Delete.Activity.Error")
                                .replace("\n", System.getProperty("line.separator")),
                        NotifyDescriptor.ERROR_MESSAGE);
                        DialogDisplayer.getDefault().notify(alert);
                        Lookup.getDefault().lookup(ILoggingService.class).error(this.getClass(), ex);
                    }
                }
            } else {
                NotifyDescriptor.Message alert = new NotifyDescriptor.Message(
                        bundle.getString("NoController.Error")
                                .replace("\n", System.getProperty("line.separator")),
                        NotifyDescriptor.ERROR_MESSAGE);
                DialogDisplayer.getDefault().notify(alert);
                Lookup.getDefault().lookup(ILoggingService.class).error(this.getClass(),
                        bundle.getString("NoController.Error").replace("\n", System.getProperty("line.separator")));
            }
        }
    }
    
    private TopComponent findActivityEditorComponent(Activity activity) {
        Set<TopComponent> openComponents = WindowManager.getDefault().getRegistry().getOpened();
        for(TopComponent comp : openComponents) {
            if(comp.getLookup().lookup(Activity.class) == activity) {
                return comp;
            }
        }
        return null;
    }
}
